{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>大纲<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#collections模块功能\" data-toc-modified-id=\"collections模块功能-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>collections模块功能</a></span><ul class=\"toc-item\"><li><span><a href=\"#collections.deque\" data-toc-modified-id=\"collections.deque-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>collections.deque</a></span></li><li><span><a href=\"#创建一个环形缓冲区的方法\" data-toc-modified-id=\"创建一个环形缓冲区的方法-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>创建一个环形缓冲区的方法</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# collections模块功能\n",
    "实现了指定数据类型的容器，作为Python一般数据类型的补充，比如：dict、list、set、tuple。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## collections.deque\n",
    "实现了快速压栈出栈的容器，[参考](https://docs.python.org/3/library/collections.html#collections.deque)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-03T01:53:09.427978Z",
     "start_time": "2018-05-03T01:53:09.419775Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque(['wang', 'kai', 'xuan'])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import deque\n",
    "d = deque(['wang','kai','xuan'])\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-03T01:53:09.434814Z",
     "start_time": "2018-05-03T01:53:09.429928Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque([\"I'm\", 'wang', 'kai', 'xuan'])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.appendleft(\"I'm\" )\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-03T01:53:09.444861Z",
     "start_time": "2018-05-03T01:53:09.436793Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque([\"I'm\", 'wang', 'kai', 'xuan', 'and'])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.append('and')\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-03T01:54:07.955560Z",
     "start_time": "2018-05-03T01:54:07.948610Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque([\"I'm\", 'wang', 'kai', 'xuan', 'and', '24', 'years old'])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.extend(['24','years old'])\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建一个环形缓冲区的方法\n",
    "- 指定最大长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-03T01:58:40.307733Z",
     "start_time": "2018-05-03T01:58:40.301535Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque(['wang'])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "de = deque(maxlen = 3)\n",
    "de.append('wang')\n",
    "de"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-03T01:58:47.382518Z",
     "start_time": "2018-05-03T01:58:47.376528Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque(['wang', 'wang'])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "de.append('wang')\n",
    "de"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-03T01:58:50.130168Z",
     "start_time": "2018-05-03T01:58:50.125788Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque(['wang', 'wang', 'wang'])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "de.append('wang')\n",
    "de"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-03T01:58:52.369353Z",
     "start_time": "2018-05-03T01:58:52.365209Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque(['wang', 'wang', 'wang'])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "de.append('wang')\n",
    "de"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-03T01:59:20.056413Z",
     "start_time": "2018-05-03T01:59:20.049319Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque(['wang', '王', '王'])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "de.append('王')\n",
    "de"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-03T01:59:22.290163Z",
     "start_time": "2018-05-03T01:59:22.285807Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque(['王', '王', '王'])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "de.append('王')\n",
    "de"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda all lib",
   "language": "python",
   "name": "base"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "大纲",
   "title_sidebar": "目录",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
